/**
 * 封装Ajax请求
 */

function request(options) {
	$.ajax({
		url: options.url,
		data: options.data,
		type: options.type || 'GET',
		dataType: options.dataType || 'json',	
		success: function(response) {
			if (response.success) {
				options.success(response.data);
			} else if(response.error){
				if (options.error) { // 调用处理错误函数
					options.error(response.error);
				} else {
					response.error.msg;
				}
			} else {
				alert('未知错误');
			}
		}
	});
}


/**
 * 数字千分位处理
 * @return {String} 返回处理好的不含小数位的数字字符串
 */
function thousandth(number) {
	var int = parseInt(number)+'',
		numArray = [];
	for(var i = int.length%3,ilen = int.length;i < ilen;i+=3) {
		if (i > 0 && i < 3) {
			numArray.push(int.substring(0,i));
		}
		numArray.push(int.substring(i,i+3));
	}
	return(numArray.join(','));
}

/**
 * 提取数字小数位
 * @demicalNum {Number} 需要提取几位小数位
 * @return {String} 返回小数位字符串
 */
function demicalFormat(number,demicalNum) {
	number = number.toFixed(demicalNum).toString();
	var demical = number.substring(number.length - demicalNum);
	return demical;
}


/**
 * 日期格式化
 * @time {Number} 时间戳
 * @return {String} 
 */
function dateFormat(time) {
	// 当前时间
	var now = new Date(),
		nowYear = now.getFullYear(),
		nowMonth = now.getMonth(),
		nowDate = now.getDate(),
		nowHours = now.getHours(),
		nowMinutes = now.getMinutes(),
		nowSeconds = now.getSeconds(),
		nowMilliseconds =  now.getMilliseconds(),
		// 时间戳
		nowTime = now.getTime(),
		// 周几
		nowWeekday = now.getDay();
		// 星期日
		if (nowWeekday == 0) {
			nowWeekday = 7;
		}


		// 需要显示时间
		var date = new Date(time),
			dateYear = date.getFullYear(),
			dateMonth = date.getMonth(),
			dateDate = date.getDate(),
			dateHours = date.getHours(),
			dateMinutes = date.getMinutes(),
			// 时间戳
			dateTime = date.getTime(),
			// 周几
			dateWeekday = date.getDay();
		// 星期日
		if (dateWeekday == 0) {
				dateWeekday = 7;
		}
		
		var weekStr = ['一','二','三','四','五','六','日'];

		// 今天
		if (nowYear == dateYear && nowMonth == dateMonth && nowDate == dateDate) {
			return addzero(dateHours) + ':' + addzero(dateMinutes);
		}

		// 今天的0点时间戳，等于当前时间戳减去小时，分钟，秒，毫秒等之和
		var todayStart = nowTime - (nowHours * 60 * 60 * 1000 + nowMinutes * 60 * 1000 + nowSeconds * 1000 + nowMilliseconds);
		
		// 昨天  今天0点和需要显示时间的差小于24小时
		if (todayStart - dateTime < 24 * 60 * 60 * 1000) {
			return '昨天 ' + addzero(dateHours) + ':' + addzero(dateMinutes); 
		}

		// 本周一0点
		var thisWeekStart = todayStart - (nowWeekday - 1) * 24 * 60 * 60 * 1000;
		// 本周
		if (dateTime > thisWeekStart) {
			return '星期' + weekStr[dateWeekday - 1] + ' ' + addzero(dateHours) + ':' + addzero(dateMinutes);
		}

		// 上周一0点
		var lastWeekStart = thisWeekStart - 7 * 24 * 60 * 60 * 1000;
		// 上周
		if (dateTime > lastWeekStart) {
			return '上周' + weekStr[dateWeekday - 1] + ' ' + addzero(dateHours) + ':' + addzero(dateMinutes);
		}

		// 早于上周
		if (dateTime < lastWeekStart) {
			return dateYear + '-' + addzero(dateMonth) + '-' + addzero(dateDate) + ' ' + addzero(dateHours) + ':' + addzero(dateMinutes);
		}

}		




/**
 * 加零
 */
function addzero(number) {
	if (number < 10 ) {
		return number = '0' + number;
	} else {
		return number;
	}
}



/**
 * 非对称加密
 */

function encryption(number) {
	// 声明公钥
	var publicKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDI3d8LKBsWMacdkN/l0cI3wziS\
VVNGOM3Z1aFyZBxUlj/WP7w03NIWccmEuT8cD/mr85z+2OQCgD0Gj+0xPsgRJ4aa\
fiUcBRfXLqS6yA/RgvJQRnkfnkGQfav5vy6t1AO3cLVUnPAlhXKMsLQwCNNYfUcz\
TAw/H9ka4zsZksDo6wIDAQAB';
	// 创建RSA对象，设置公钥
	var encrypt = new JSEncrypt();
	encrypt.setPublicKey(publicKey);

	// 公钥加密
	number = encrypt.encrypt(number);
	return number;
}



/**
 * 截取字符串
 * @param {String} str
 * @param {Number} len
 * @return {String}
 */
function substr(str, len) {
    if (str.length > len) {
        return str.substring(0, len) + '...';
    } else {
        return str;
    }
}


/**
 * 获取URL参数
 */
function getParameter(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
